package zisu.netty.practice.netty.my.quanweicode;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import javax.xml.bind.Marshaller;
import java.util.List;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2022/1/14 14:25
 */
public class NettyMessageEncoder extends MessageToMessageEncoder<NettyMessage> {

    public NettyMessageEncoder(){

    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, NettyMessage nettyMessage, List<Object> list) throws Exception {
        if(nettyMessage == null || nettyMessage.getHeader() == null){
            throw new Exception("this code message is null");
        }
        ByteBuf sendBuf = Unpooled.buffer();
        sendBuf.writeInt(nettyMessage.getHeader().getCrcCode());
        sendBuf.writeInt(nettyMessage.getHeader().getLength());
        sendBuf.writeLong(nettyMessage.getHeader().getSessionId());
        sendBuf.writeByte(nettyMessage.getHeader().getType());
        sendBuf.writeByte(nettyMessage.getHeader().getPriority());
        sendBuf.writeInt(nettyMessage.getHeader().getAttachment().size());
    }
}
